# -*- encoding: utf-8 -*-
# gammel ephorte parser, like http://www.amot.kommune.no/files/amot/a-winsak/postlist/AAKO/AAKO.html
############################################################################
#    Copyright (C) 2007 by Håvard Gulldahl                                 #
#    havard@gulldahl.no                                                    #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import logging
from datetime import datetime
from time import strptime
import jeje.parser
#import jeje
import re

class parser(jeje.parser.ejeje_parser):


    def ffinn_enkeltjournal_uri(self, postjournal, suppe, dato=None):
        pass

    def parse_enkeltjournal(self, suppe, uri):
        logging.info('starter ephorte-gammel-parser')
        #self.sett_info(suppe.)
        _dato = suppe.find('font', 'UnderOverskrift').string.split(':')[-1]
        self.sett_info(datetime(*strptime(_dato, '%d.%m.%Y')[0:6]))
        logging.debug('finner tittelposter i suppa')
        _tittelposter = suppe.findAll('table')
        logging.debug('fant %d titler', len(_tittelposter))
        for i, tp in enumerate(_tittelposter):

            innhold = tp.findAll('tr')
            #logging.debug('innhold: %s', innhold)
            tittel = innhold[1].td.string
            logging.debug('arbeider med post #%d: %s', i, tittel)
            post = self.legg_til_post()
            post.sett_tittel(unicode(tittel))
            post.sett_sakskompleks(innhold[3].td.string)

            retning, doknr, refnr, journaldato, dokdato, saksbehandler = [ x.string for x in innhold[5].findAll('td') ]

            post.sett_retning(retning)
            post.sett_dokumentnr(unicode(doknr))
            post.sett_refnr(unicode(refnr))
            post.sett_arkivdato(datetime(*strptime(journaldato[:10], '%d.%m.%Y')[0:6]))
            try:
                post.sett_dokumentdato(datetime(*strptime(dokdato[:10], '%d.%m.%Y')[0:6]))
            except ValueError:
                pass
            post.sett_saksbehandler(unicode(saksbehandler))

            logging.debug([ x.string for x in innhold[7].findAll('td') ])
            kode, hjemmel, vurdert, klassering = [ x.string for x in innhold[7].findAll('td') ]
            if klassering is not None:
                post.sett_kommentar(klassering)

            logging.debug([ x.string for x in innhold[9].findAll('td') ])
            type_adressat, adressat, avskrdato, avskr, besvar= [ x.string for x in innhold[9].findAll('td') ]

            post.sett_adressat(unicode(adressat))
            if hjemmel is not None:
                post.sett_lovreferanse(unicode(hjemmel))
            post.kontroller_felter()
        return self.journalposter

